import types from './types'
import { Fetch } from '@/utils/http_common'

const actions = {
	// 获取订单
	setOrders({ commit, state }, params) {
		commit(types.UPDATE_PARAMS, params)
		Fetch.get('search_orders', { params: state.params }).then(res => {
			commit(types.SET_ORDERS, {
				orders: res.orders,
				total: res.page.total_count
			})
		})
	},
	// 切换订单的类型
	updateParamsType({ dispatch, state }, type) {
		const params = { type: type, page: 1 }
		dispatch('setOrders', params)
	},

	// 单纯的更新参数的方法
	updateParams({ commit, state }, params) {
		commit(types.UPDATE_PARAMS, params)
	},

	// 获取测量师傅
	setMasters({ commit, state }) {
		Fetch.get('users/fetch_master').then(res => {
			commit(types.SET_MASTERS, res.users)
		})
	},

	// 获取所有的品牌
	setCompanies({ commit, state }) {
		Fetch.get('fetch_source').then(res => {
			commit(types.SET_COMPANIES, res.company)
		})
	},

	// 设置测量师傅
	setCustomerMaster({ commit }, id) {
		let params
		if(id) {
			params = { customer_service_id: id }
		} else {
			params = { customer_service_id: null }
		}
		commit(types.UPDATE_PARAMS, params)
	},

	// 设置安装师傅
	setFacilitatorMaster({ commit }, id) {
		let params
		if(id) {
			params = { facilitator_id: id }
		} else {
			params = { facilitator_id: null }
		}
		commit(types.UPDATE_PARAMS, params)
	},

	// 设置品牌
	setCompany({ commit }, id) {
		let params
		if(id) {
			params = { company_id: id }
		} else {
			params = { company_id: null }
		}
		commit(types.UPDATE_PARAMS, params)
	},

	// 设置 show 页面的当前order
	setOrder({ commit, dispatch }, payload) {
		if(payload.order) {
			commit(types.SET_ORDER, payload.order)
			dispatch('setAuditUsers', payload.order)
		} else {
			Fetch.get('orders/' + payload.id).then(res => {
				commit(types.SET_ORDER, res.order)
				dispatch('setAuditUsers', res.order)
			})
		}
	},

	// 更新订单信息 {id: xxx, attributes: {}}
	updateOrder({ commit, dispatch }, payload) {
		return new Promise((resolve, reject) => {
			Fetch.put('orders/' + payload.id, payload.attributes).then((res) => {
				commit(types.SET_ORDER, res.order)
				resolve(true)
			}).catch((error) => {
				console.log(error)
				reject(false)
			})
		})
	},

	// 设置 材料select的数据
	setMaterials({ commit }) {
		Fetch.get('fetch_source').then(res => {
			commit(types.SET_MATERIALS, res.material)
		})
	},

	// 上传图片
	uploadImg({ commit, state }, payload) {
		return new Promise((resolve, reject) => {
			Fetch.post('orders/' + payload.id + '/upload_data', payload.data).then(res => {
				commit(types.SET_ORDER, res.order)
				console.log(state)
				resolve(true)
			}).catch((error) => {
				console.log('上传图片action', error)
				reject(false)
			})
		})
	},

	// 设置 审核人员
	setAuditUsers({ commit, dispatch }, order) {
		Fetch.get('orders/' + order.id + '/fetch_audit_users', { params: { type: 'web' } }).then(res => {
			var auitList=[]
			var flowsList=[]
			if(res.company_user) {
				for(var k in res.result) {
					auitList.push(res.result[k])
				}
				auitList.push(res.company_user)
			}
			if(order.flows.length!=0){
				for(var t=order.flows.length-1;t>=0;t--){
					if(flowsList.indexOf(order.flows[t])==-1){
						flowsList.push(order.flows[t])
					}
				}
			}
			for(var i=0,j=0;i < auitList.length, j < flowsList.length;i++,j++){
				if(flowsList[j].user_id==auitList[i].id){
					auitList[i].confirm=flowsList[j].confirm
					auitList[i].updated_at=flowsList[j].updated_at
				}
			}
			commit(types.SET_AUDIT_USERS, auitList)
		})
	},

	updateSlideAction({ commit }, payload) {
	    commit(types.UPDATE_ORDER_TAB,payload)
	}
}

export default actions